package com.originskyseed.platform.role.provider;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.role.mapper.RoleRelUserMapper;
import com.originskyseed.platform.role.model.RoleRelUser;
import com.originskyseed.platform.role.service.IRoleRelUserService;

/**
 * 角色与用户关系表Service接口实现类（OSI_ROLE_REL_USER）
 * 
 * @author LiangMingHui
 */
@DubboService
@Component("roleRelUserService")
public class RoleRelUserServiceImpl implements IRoleRelUserService {

	@Resource
	private RoleRelUserMapper roleRelUserMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.roleRelUserMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.roleRelUserMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(RoleRelUser record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.roleRelUserMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(RoleRelUser record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.roleRelUserMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(RoleRelUser record) throws Exception {
		record.setUpdateTime(new Date());
		return this.roleRelUserMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(RoleRelUser record) throws Exception {
		record.setUpdateTime(new Date());
		return this.roleRelUserMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public RoleRelUser selectByPrimaryKey(String id) throws Exception {
		return this.roleRelUserMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RoleRelUser> selectList(RoleRelUser record) throws Exception {
		return this.roleRelUserMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<RoleRelUser> selectListByPage(Map<String, Object> params, BasePage<RoleRelUser> page) throws Exception {
		params.put("page", page);
		List<RoleRelUser> list = this.roleRelUserMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 根据用户标识重新关联记录
	 * 
	 * @param userId
	 * @param roleIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteAndInsertByUserId(String userId, List<String> roleIds) throws Exception {
		// 根据用户标识删除关联记录
		int result = this.roleRelUserMapper.deleteByUserId(userId);

		// 重新插入用户与角色关联记录
		for (String roleId : roleIds) {
			RoleRelUser roleRelUser = new RoleRelUser();
			roleRelUser.setRoleId(roleId);
			roleRelUser.setUserId(userId);
			result += this.insert(roleRelUser);
		}
		return result;
	}

	/**
	 * 根据角色标识重新关联记录
	 * 
	 * @param roleId
	 * @param userIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteAndInsertByRoleId(String roleId, List<String> userIds) throws Exception {
		// 根据用户标识删除关联记录
		int result = this.roleRelUserMapper.deleteByRoleId(roleId);

		// 重新插入用户与角色关联记录
		for (String userId : userIds) {
			RoleRelUser roleRelUser = new RoleRelUser();
			roleRelUser.setRoleId(roleId);
			roleRelUser.setUserId(userId);
			result += this.insert(roleRelUser);
		}
		return result;
	}

	/**
	 * 根据用户标识获取关联记录
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RoleRelUser> selectByUserId(String userId) throws Exception {
		return this.roleRelUserMapper.selectByUserId(userId);
	}

	/**
	 * 根据角色标识获取关联记录
	 * 
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RoleRelUser> selectByRoleId(String roleId) throws Exception {
		return this.roleRelUserMapper.selectByRoleId(roleId);
	}

	/**
	 * 是否存在用户关联记录
	 * 
	 * @param roleIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean existUserId(List<String> roleIds) throws Exception {
		return this.roleRelUserMapper.existUserId(roleIds);
	}

	/**
	 * 是否存在用户关联记录
	 *
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean existUserId(String roleId) throws Exception {
		return this.roleRelUserMapper.existUserIdByRoleId(roleId);
	}
}